<template>
	<div>
		<div class="h2">角色管理</div>
		<div id="components-form-demo-advanced-search">
			<a-form class="ant-advanced-search-form home-form" :form="form" @submit="handleSearch">
				<a-row :gutter="24">
					<a-col :span="8">
						<a-form-item label="名称"><a-input class="field-right" placeholder="请输入名称搜索" v-decorator="[`key`]" /></a-form-item>
					</a-col>
					<a-col :span="8" style="margin-top:-15px;">
						<a-button-group size="default" class="option">
							<a-button-group size="default" class="option">
								<a-button type="primary" @click="handleSearch">搜索</a-button>
								<a-button @click="handleReset">重置</a-button>
								<a-button type="primary" @click="addList">新增</a-button>
							</a-button-group>
						</a-button-group>
					</a-col>
				</a-row>
			</a-form>
		</div>
		<div>
			<el-table :data="data" v-loading="loading" border :max-height="tableHeight" highlight-current-row style="width: 100%;">
				<el-table-column type="index" label="序号" width="60"></el-table-column>
				<el-table-column prop="Name" label="名称" min-width="130"></el-table-column>
				<el-table-column prop="AddTime" label="添加时间">
					<template slot-scope="scope">
						<a-tag color="pink">{{ scope.row.AddTime }}</a-tag>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="300">
					<template slot-scope="scope">
						<oprateBtn title="修改" icon="icon-editor1" color="#1890FF" @tap="modifyList(scope.row,'modify')"></oprateBtn>
						<!-- <oprateBtn title="活动审批权限" icon="icon-guanli" color="green" @tap="seeList(scope.row,'RoleActivity')"></oprateBtn>
						<oprateBtn title="积分申请审批权限" icon="icon-huodong" color="green" @tap="seeList(scope.row,'RoleCreditFlow')"></oprateBtn>
						<oprateBtn title="积分发放审批权限" icon="icon-xueyuanyishenpi" color="green" @tap="seeList(scope.row,'RoleCreditSendExam')"></oprateBtn>
						<oprateBtn title="假期社会实践审批权限" icon="icon-guanli" color="blue" @tap="seeList(scope.row,'RolePracticeTeam')"></oprateBtn>
						<oprateBtn title="活动管理权限" icon="icon-shezhi" color="blue" @tap="seeList(scope.row,'RoleActivityManage')"></oprateBtn>
						<oprateBtn title="角色模块权限" icon="icon-guanliyuan" color="red" @tap="seeList(scope.row,'RoleModule')"></oprateBtn>
						<oprateBtn title="组织年审审批权限" icon="icon-huodong" color="#1890FF" @tap="seeList(scope.row,'RoleClubAnnualAuditFlow')"></oprateBtn>
						 --><oprateBtn title="删除" icon="icon-delete" color="red" @tap="deleteList(scope.row)"></oprateBtn>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				:current-page="pagination.currentPage"
				:page-sizes="pagination.pageSizeOptions"
				:page-size="pagination.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="pagination.total"
			></el-pagination>
		</div>
		<add :text="text" :RoleList="RoleList" :timer="timer" :treeDate="RoleList" v-show="addIf" @closeFun="closeFun"></add>
	</div>
</template>

<script>
import add from './add';
import Func from '@/utils/func';
import { GetRolePageList, DeleteRole, GetPowerData } from '@/api/follow';
const data = [];
export default {
	components: {
		add
	},
	data() {
		return {
			data,
			timer:0,
			form: this.$form.createForm(this, {
				name: 'advanced_search'
			}),
			pagination: {
				currentPage: 1,
				pageSize: this.global.pageSize,
				pageSizeOptions: this.global.pageSizeOptions, // 每页中显示的数据
				showTotal: total => `共有 ${total} 条数据` // 分页中显示总的数据
			},
			addIf: false,
			text: {},
			visible: false,
			confirmLoading: false,
			itemId: '',
			departmentList: [],
			worklist: [],
			loading: false,
			values: {},
			pages: {
				pageIndex: 2,
				pageSize: 20
			},
			tableHeight: parseFloat(window.innerHeight - 160),
			RoleList: []
		};
	},
	computed: {},
	created() {
		this.getList();
		this.getOtherList();
	},
	mounted() {},
	methods: {
		handleSearch(e) {
			e.preventDefault();
			this.form.validateFields((error, values) => {
				this.pagination.currentPage = 1;
				this.values = values;
				this.getList();
			});
		},
		handleReset() {
			this.form.resetFields();
			this.values = {};
			this.getList();
		},
		handleSizeChange(val) {
			this.pagination.pageSize = val;
			this.getList();
		},
		handleCurrentChange(val) {
			this.pagination.currentPage = val;
			this.getList();
		},
		async getList() {
			this.loading = true;
			let data = {};
			if (this.values) {
				data = this.values;
			}
			data.pageIndex = this.pagination.currentPage;
			data.pageSize = this.pagination.pageSize;
			let res = await GetRolePageList(data);
			this.loading = false;
			this.data = res.data.data;
			const pagination = { ...this.pagination };
			pagination.total = res.data.pageInfo.TotalItemCount;
			this.pagination = pagination;
		},
		async getOtherList() {
			let res = await GetPowerData();
			let RoleList = res.data.data
			let str = JSON.stringify(RoleList)
			str = str.replace(/GroupData/g, "children")
			str = str.replace(/Functionchildren/g, "children")
			str = str.replace(/FunctionData/g, "children")
			str = str.replace(/Name/g, "title")
			str = str.replace(/ID/g, "value")
			this.RoleList = JSON.parse(str)
			// this.RoleList = [
			// 	{
			// 		title: 'Node1',
			// 		value: '1',
			// 		// key: '0-0',
			// 		children: [
			// 			{
			// 				value: '2',
			// 				// key: '0-0-1',
			// 				title: 'title'
			// 			},
			// 			{
			// 				title: 'Child Node2',
			// 				value: '3',
			// 				// key: '0-0-2'
			// 			}
			// 		]
			// 	},
			// 	{
			// 		title: 'Node2',
			// 		value: '4',
			// 		// key: '0-1'
			// 	}
			// ];
		},
		addList() {
			this.text = {};
			this.timer = new Date().getSeconds()
			this.addIf = !this.addIf;
		},
		modifyList(text) {
			text = Object.assign(text, { time: new Date() });
			this.text = text;
			this.timer = new Date().getSeconds()
			this.addIf = !this.addIf;
		},
		enableList(text) {
			let that = this;
			this.$confirm({
				title: '提示',
				content: `您确定要${text.IsEnable ? '停用' : '启用'}该学生组织吗？`,
				okText: '确认',
				cancelText: '取消',
				async onOk() {
					let data = {};
					data.id = text.ID;
					let res = await EnableClub(data);
					if (res.data.code == 0) {
						that.getList();
						that.$message.success(res.data.msg);
					} else {
						that.$message.error(res.data.msg);
					}
				}
			});
		},
		deleteList(text) {
			let that = this;
			this.$confirm({
				title: '提示',
				content: `您确定删除该角色吗？`,
				okText: '确认',
				cancelText: '取消',
				async onOk() {
					let data = {};
					data.id = text.ID;
					let res = await DeleteRole(data);
					if (res.data.code == 0) {
						that.getList();
						that.$message.success(res.data.msg);
					} else {
						that.$message.error(res.data.msg);
					}
				}
			});
		},
		seeList(text,name){
			this.$router.push({
				name: name,
				query: {
					roleID: text.ID
				}
			});
		},
		closeFun(data) {
			this.addIf = false;
			if(data==1){
				this.getList();
			}
		}
	}
};
</script>

<style lang="less" scoped>
.home-form /deep/.ant-form-item {
	display: flex;
	margin-bottom: 10px !important;
}
.home-form /deep/.ant-form-item-label {
		width: 80px;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		text-align: right;
	}
.home-form /deep/.ant-form-item-control-wrapper {
		flex: 1;
	}
/deep/.el-table th.is-leaf {
	background: #fafafa;
}
/deep/.el-table td,
/deep/.el-table th {
	padding: 5px;
}
.option {
	margin-top: 10px;
	margin-bottom: 20px;
	overflow: hidden;
}
.option .btn {
	height: 36px;
	line-height: 36px;
	padding: 0 10px;
	float: left;
	background: #1890ff;
	color: #fff;
	margin-right: 10px;
	border-radius: 3px;
	cursor: pointer;
	border: none;
}
.foot {
	text-align: center;
	margin-top: 100px;
}
.el-pagination {
	margin-top: 10px;
}
</style>
